<script setup>
import {ref} from "vue";
import { useRouter, useRoute } from "vue-router";
import {ElLoading, ElMessage} from "element-plus";
import {http} from "@/api";
import Editor from "@/components/Editor.vue";
const apiKey = 'ProductList'
const router = useRouter()
const route = useRoute()
const id = route.params.id
const categoryList = ref([])
http.Category.list().then(res => {
  categoryList.value = res.data
})
const formData = ref({
  id: undefined,
  category_id: '',
  image: '',
  name: '',
  detail: '',
  tips: '',
  status: 1,
  is_recommend: 0,
  price: '',
  store_count: '',
  sale_count: '',
  collect_count: '',
})
const rules = ref({
  name: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  category_id: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
})

const formRef =  ref(null)
if (id) {
  // 获取详情
  http[apiKey].read(id).then(res => {
    for (const valueKey in formData.value) {
      formData.value[valueKey] = res.data[valueKey]
    }
    formData.value.id = id
  })
}
const cancel = () => {
  router.back()
}
const confirm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      const loadingInstance = ElLoading.service()
      if (id) {
        // 编辑
        http[apiKey].update({
          ...formData.value,
          id
        }).then(res => {
          ElMessage.success('编辑成功')
          router.back()
        }).finally(() => {
          loadingInstance.close()
        })
      } else {
        // 新增
        http[apiKey].create(formData.value).then(res => {
          ElMessage.success('新增成功')
          router.back()
        }).finally(() => {
          loadingInstance.close()
        })
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<template>
  <PageMain :title="id ? '编辑' :'新增'">
    <div class="form-content f-c ">
      <div class="form-box form-box2">
        <el-form ref="formRef" style="width: 660px" :model="formData" :rules="rules" label-position="top" >
         <el-row :gutter="20">
           <el-col :span="12">
             <el-form-item label="名称" prop="name">
               <el-input clearable v-model="formData.name"></el-input>
             </el-form-item>
           </el-col>
            <el-col :span="12">
              <el-form-item label="分类" prop="category_id">
                <el-tree-select v-model="formData.category_id"  default-expand-all :data="categoryList" :props="{ label: 'name', value: 'id'}" placeholder="请选择">
                </el-tree-select>
              </el-form-item>
            </el-col>
           <el-col :span="12">
             <el-form-item label="价格" prop="price">
               <el-input clearable v-model="formData.price"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="销量" prop="sale_count">
               <el-input clearable v-model="formData.sale_count"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="库存" prop="store_count">
               <el-input clearable v-model="formData.store_count"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="收藏数量" prop="collect_count">
               <el-input clearable v-model="formData.collect_count"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="图片" style="width: 100%" prop="image">
               <UploadImage v-model:image="formData.image"/>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="提示信息" style="width: 100%" prop="tips">
               <el-input clearable v-model="formData.tips"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="上架状态">
               <el-switch :active-value="1" :inactive-value="0" v-model="formData.status"/>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="是否推荐">
               <el-switch :active-value="1" :inactive-value="0" v-model="formData.is_recommend"/>
             </el-form-item>
           </el-col>
           <el-col :span="24">
             <el-form-item label="详情" style="width: 100%" prop="remark">
               <Editor v-model="formData.detail"/>
             </el-form-item>
           </el-col>
         </el-row>
        </el-form>
      </div>
      <div class="form-button f-s">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </div>
    </div>
  </PageMain>
</template>
<style lang="scss">
.form-box2 {
  display: flex;
  .gap {
    width: 32px;
  }
}
</style>
